package com.ofc.chart.builder;

import java.util.List;

import com.ofc.chart.element.Element;
import com.ofc.chart.element.Style;
import com.ofc.chart.element.Title;
import com.ofc.chart.element.XAxis;
import com.ofc.chart.element.YAxis;
import com.ofc.chart.element.YLegend;

/**
 * Chart所需元素的构建类
 * 
 * @author zhangpeng
 * @date 2012-12-6
 */
public class ElementBuilder {
	public String buildTitle(Title title) {
		StringBuffer json = new StringBuffer("\"title\":{");
		if (title == null) {
			return null;
		} else {
			String text = buildTitleText(title.getText());
			boolean hasText = false;
			if (text != null && !"".equals(text)) {
				json.append(text);
				hasText = true;
			}
			String style = buildTitleStyle(title.getStyle());
			boolean hasStyle = false;
			if (style != null && !"".equals(style)) {
				if (hasText) {
					json.append(",");
				}
				json.append(style);
				hasStyle = true;
			}
			if (!hasText && !hasStyle) {
				return null;
			}
		}
		json.append("}");
		return json.toString();
	}

	public String buildYLegend(YLegend ylegend) {
		StringBuffer json = new StringBuffer("\"y_legend\":{");
		if (ylegend == null) {
			return null;
		} else {
			String text = buildTitleText(ylegend.getText());
			boolean hasText = false;
			if (text != null && !"".equals(text)) {
				json.append(text);
				hasText = true;
			}
			String style = buildTitleStyle(ylegend.getStyle());
			boolean hasStyle = false;
			if (style != null && !"".equals(style)) {
				if (hasText) {
					json.append(",");
				}
				json.append(style);
				hasStyle = true;
			}
			if (!hasText && !hasStyle) {
				return null;
			}
		}
		json.append("}");
		return json.toString();
	}

	public String buildTitleText(String text) {
		StringBuffer json = new StringBuffer("\"text\":");
		if (text == null) {
			return null;
		}
		json.append("\"" + text + "\"");
		return json.toString();
	}

	public String buildTitleStyle(Style style) {
		StringBuffer json = new StringBuffer("\"style\":");
		StringBuffer styleJson = new StringBuffer("{");
		if (style == null) {
			return null;
		} else {
			if (style.getFontSize() != null && !"".equals(style.getFontSize())) {
				styleJson.append("font-size:" + style.getFontSize() + ";");
			}
			if (style.getFontFamily() != null
					&& !"".equals(style.getFontFamily())) {
				styleJson.append("font-family:" + style.getFontFamily() + ";");
			}
			if (style.getTextAlign() != null
					&& !"".equals(style.getTextAlign())) {
				styleJson.append("text-align:" + style.getTextAlign() + ";");
			}
			if (style.getColor() != null && !"".equals(style.getColor())) {
				styleJson.append("color:" + style.getColor() + ";");
			}
			if (style.getPadding() != null && !"".equals(style.getPadding())) {
				styleJson.append("padding:" + style.getPadding() + ";");
			}
		}
		styleJson.append("}");
		json.append("\"" + styleJson.toString() + "\"");
		return json.toString();
	}

	public String buildXAxis(XAxis axis) {
		StringBuffer json = new StringBuffer("\"x_axis\":{");
		if (axis == null) {
			return null;
		} else {
			boolean hasStroke = false;
			boolean hasTickHeight = false;
			boolean hasColour = false;
			boolean hasGridColour = false;
			boolean hasLabels = false;
			boolean has3D = false;
			if (axis.getStroke() != null && !"".equals(axis.getStroke())) {
				json.append("\"stroke\":" + axis.getStroke() + ",");
				hasStroke = true;
			}
			if (axis.getTickHeight() != null
					&& !"".equals(axis.getTickHeight())) {
				json.append("\"tick_height\":" + axis.getTickHeight() + ",");
				hasTickHeight = true;
			}
			if (axis.getColour() != null && !"".equals(axis.getColour())) {
				json.append("\"colour\":\"" + axis.getColour() + "\",");
				hasColour = true;
			}
			if (axis.getGridColour() != null
					&& !"".equals(axis.getGridColour())) {
				json.append("\"grid_colour\":\"" + axis.getGridColour() + "\",");
				hasGridColour = true;
			}
			if (axis.getLabels() != null && axis.getLabels().size() > 0) {
				json.append("\"labels\":{");
				json.append("\"labels\":[");
				for (String value : axis.getLabels()) {
					json.append("\"" + value + "\",");
				}
				json = new StringBuffer(filterLastChar(json.toString(), ","));
				json.append("]");
				json.append("},");
				hasLabels = true;
			}
			if (axis.get3d() != null && !"".equals(axis.get3d())) {
				json.append("\"3d\":" + axis.get3d() + "");
				has3D = true;
			}
			json = new StringBuffer(filterLastChar(json.toString(), ","));
			if (!hasStroke && !hasTickHeight && !hasColour && !hasGridColour
					&& !hasLabels && !has3D) {
				return null;
			}
		}
		json.append("}");
		return json.toString();
	}

	public String buildYAxis(YAxis axis) {
		StringBuffer json = new StringBuffer("\"y_axis\":{");
		if (axis == null) {
			return null;
		} else {
			boolean hasStroke = false;
			boolean hasTickHeight = false;
			boolean hasColour = false;
			boolean hasGridColour = false;
			boolean hasOffset = false;
			boolean hasMax = false;
			if (axis.getStroke() != null && !"".equals(axis.getStroke())) {
				json.append("\"stroke\":" + axis.getStroke() + ",");
				hasStroke = true;
			}
			if (axis.getTickHeight() != null
					&& !"".equals(axis.getTickHeight())) {
				json.append("\"tick_height\":" + axis.getTickHeight() + ",");
				hasTickHeight = true;
			}
			if (axis.getColour() != null && !"".equals(axis.getColour())) {
				json.append("\"colour\":\"" + axis.getColour() + "\",");
				hasColour = true;
			}
			if (axis.getGridColour() != null
					&& !"".equals(axis.getGridColour())) {
				json.append("\"grid_colour\":\"" + axis.getGridColour() + "\",");
				hasGridColour = true;
			}
			if (axis.getOffset() != null && !"".equals(axis.getOffset())) {
				json.append("\"offset\":" + axis.getOffset() + ",");
				hasOffset = true;
			}
			if (axis.getMax() != null && !"".equals(axis.getMax())) {
				json.append("\"max\":" + axis.getMax() + "");
				hasMax = true;
			}
			json = new StringBuffer(filterLastChar(json.toString(), ","));
			if (!hasStroke && !hasTickHeight && !hasColour && !hasGridColour
					&& !hasOffset && !hasMax) {
				return null;
			}
		}
		json.append("}");
		return json.toString();
	}

	public String buildElements(List<Element> elementList) {
		StringBuffer json = new StringBuffer("\"elements\":[");
		if (elementList == null || elementList.size() == 0) {
			return null;
		} else {
			for (Element element : elementList) {
				boolean hasType = false;
				boolean hasAlpha = false;
				boolean hasColour = false;
				boolean hasText = false;
				boolean hasFontSize = false;
				boolean hasStartAngle = false;
				boolean hasStroke = false;
				boolean hasAnimate = false;
				boolean hasWidth = false;
				boolean hasDotSize = false;
				boolean hasTip = false;
				boolean hasValues = false;
				json.append("{");
				if (element.getType() != null && !"".equals(element.getType())) {
					json.append("\"type\":\"" + element.getType() + "\",");
					hasType = true;
				}
				if (element.getAlpha() != null
						&& !"".equals(element.getAlpha())) {
					json.append("\"alpha\":" + element.getAlpha() + ",");
					hasAlpha = true;
				}
				List<String> colours = element.getColours();
				if (colours != null && colours.size() > 0) {
					if (colours.size() == 1) {
						String colour = colours.get(0);
						if (colour != null && !"".equals(colour)) {
							json.append("\"colour\":\"" + colour + "\",");
							hasColour = true;
						}
					} else {
						json.append("\"colours\":[");
						for (String colour : colours) {
							json.append("\"" + colour + "\",");
						}
						json = new StringBuffer(filterLastChar(json.toString(),
								","));
						json.append("],");
						hasColour = true;
					}
				}
				if (element.getText() != null && !"".equals(element.getText())) {
					json.append("\"text\":\"" + element.getText() + "\",");
					hasText = true;
				}
				if (element.getFontSize() != null
						&& !"".equals(element.getFontSize())) {
					json.append("\"font-size\":" + element.getFontSize() + ",");
					hasFontSize = true;
				}
				if (element.getStartAngle() != null
						&& !"".equals(element.getStartAngle())) {
					json.append("\"start-angle\":" + element.getStartAngle()
							+ ",");
					hasStartAngle = true;
				}
				if (element.getStroke() != null
						&& !"".equals(element.getStroke())) {
					json.append("\"stroke\":" + element.getStroke() + ",");
					hasStroke = true;
				}
				if (element.getAnimate() != null
						&& !"".equals(element.getAnimate())) {
					json.append("\"animate\":" + element.getAnimate() + ",");
					hasAnimate = true;
				}
				if (element.getWidth() != null
						&& !"".equals(element.getWidth())) {
					json.append("\"width\":" + element.getWidth() + ",");
					hasWidth = true;
				}
				if (element.getDotSize() != null
						&& !"".equals(element.getDotSize())) {
					json.append("\"dot-size\":" + element.getDotSize() + ",");
					hasDotSize = true;
				}
				if (element.getTip() != null && !"".equals(element.getTip())) {
					json.append("\"tip\":\"" + element.getTip() + "\",");
					hasTip = true;
				}
				if (element.getValues() != null
						&& element.getValues().size() > 0) {
					json.append("\"values\":[");
					for (String value : element.getValues()) {
						json.append("" + value + ",");
					}
					json = new StringBuffer(
							filterLastChar(json.toString(), ","));
					json.append("]");
					hasValues = true;
				}
				json = new StringBuffer(filterLastChar(json.toString(), ","));
				json.append("},");
				if (!hasType && !hasAlpha && !hasColour && !hasText
						&& !hasFontSize && !hasValues && !hasStartAngle
						&& !hasStroke && !hasAnimate && !hasWidth
						&& !hasDotSize && !hasTip) {
					json = new StringBuffer(filterLastChar(json.toString(),
							"{},"));
				}
			}
			json = new StringBuffer(filterLastChar(json.toString(), ","));
		}
		json.append("]");
		return json.toString();
	}

	public String filterLastChar(String str, String filter) {
		int offset = filter.length();
		if (str.lastIndexOf(filter) == str.length() - offset) {
			str = str.substring(0, str.length() - offset);
		}
		return str;
	}

}
